package com.gy.spark.sparksql.dataframe

import org.apache.spark.{SparkConf, SparkContext}
import org.apache.spark.sql.SQLContext


case class Person(id: String, name: String, age: Integer)

object CreateDFFromRDDWithReflect {
  def main(args: Array[String]): Unit = {
    val conf = new SparkConf()
    conf.setMaster("local").setAppName(this.getClass.getSimpleName)
    val sc = new SparkContext(conf)
    val sqlContext = new SQLContext(sc)
    val lineRDD = sc.textFile("./spark/input/sql/person.txt")

    /**
     * 将RDD隐式转换成DataFrame
     */
    import sqlContext.implicits._

    val personRDD = lineRDD.map { x =>
      Person(x.split(",")(0), x.split(",")(1), Integer.valueOf(x.split(",")(2)))
    }

    val df = personRDD.toDF();
    df.show()

    /**
     *
     */
    val rdd = df.rdd
    val result = rdd.map { x => {
      Person(x.getAs("id"), x.getAs("name"), x.getAs("age"))
    }
    }
    result.foreach {
      println
    }
    sc.stop()
  }
}